<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page import="ca.benow.transmission.model.*"%>
<%@ page import="ca.benow.transmission.model.TorrentStatus.TorrentFile"%>
<%@ page import="java.util.*"%>
<%@ page import=" java.text.DecimalFormat"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link type="text/css" href="css/ui-darkness/jquery-ui-1.8.16.custom.css"
	rel="stylesheet" />
<link type="text/css" href="css/torrent.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="js/torrents.js"></script>
<title>Torrents</title>
</head>
<body>
<%!	
	private String printTorrentFileSize(TorrentFile file) {
		long size = file.getSize();
		return prettifysize(size);
	}

	private String prettifysize(long size){
		Double mod = ((Long)size).doubleValue();
		
		
		int modulo;
		for(modulo = 0;modulo<3;modulo++){
			if((mod/1024)>=1){
				mod = mod/1024;
			}else{
				break;
			}
		}
		DecimalFormat df = new DecimalFormat("#.##");
		String value= df.format(mod);
		//System.out.println("Size: "+String.valueOf(size)+" | Modulo: "+String.valueOf(modulo));
		String unity ="";
		switch(modulo){
			case 0:
				unity = value.concat(" o");
				break;
			case 1://ko
				unity = value.concat(" Ko");
				break;
			case 2://Mo
				unity = value.concat(" Mo");
				break;
			case 3://Go
				unity = value.concat(" Go");
				break;
			default :
				unity = "> ".concat(value).concat(" Go");
		}
		return unity;
	}
%>
<%
//Get the list object of  torrents 
List<TorrentStatus> list = (List<TorrentStatus>) request.getAttribute("torrentList"); 
int i=0;

%>


<s:if test="%{torrentList.size()>0}">
	<div id="torrentsList" class="ui-corner-all">
	<s:iterator value="torrentList">
		
		<div class="torrent  <% if(i%2==0) out.print("line"); %>">
		<div class="torrent_name" id="<s:property value="id" />"><s:property value="name" /></div>
		<div class="torrent_status" id="statuts_<s:property value="id" />">status : <s:property value="status" /></div>
		<div class="torrent_percent" id="percent_<s:property value="id" />"><div class="progressbar" id="progressbar_<s:property value="id" />" value="<s:property value="percentDone" />"></div>
		</div>
		<div class="torrent_size" id="size_<s:property value="id" />">Size : <%=prettifysize(list.get(i).getSize()) %></div>
		<div>
		<button class="files_header" id="<s:property value="id" />">Display Files</button>
		<div class="file_list ui-corner-all-small invisible " id="file_list_<s:property value="id" />">
		<% 
			try {
				int smalli =0;
				ArrayList<TorrentFile> files = list.get(i).getFiles();
				for(TorrentFile file : files){
					%>
					<div class="file_unit <% if(smalli%2!=0) out.print("small_line_grey"); else out.print("small_line_white"); %>">
						<span class="file_name ellipsis" title="<%=file.getName()%>"><%=file.getName()%></span> <span class="file_size"><%=printTorrentFileSize(file) %></span>							
					</div>
					 <%
					 smalli++;
				}
			}catch(Exception ex){
				System.out.println(ex.getMessage());
			}
		%>
		</div>
		</div>
		<%i++; %>
		</div>
	</s:iterator></div>
</s:if>
<s:else>
 	Aucun torrents 
</s:else>


</body>
</html>